package com.briup.bookstore.web.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.briup.bookstore.dto.OrderCreateDTO;
import com.briup.bookstore.dto.OrderPayDTO;
import com.briup.bookstore.dto.OrderPaySuccessDTO;
import com.briup.bookstore.dto.OrderSearchDTO;
import com.briup.bookstore.response.Result;
import com.briup.bookstore.service.OrderService;
import com.briup.bookstore.vo.OrderDetailVO;
import com.briup.bookstore.vo.OrderInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/7/26 下午2:57
 * @Description 订单模块
 **/

@RestController
@RequestMapping("order")
@Api(tags = "订单模块")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AlipayClient alipayClient;

    @PostMapping
    @ApiOperation("创建订单")
    public Result createOrder(@RequestBody OrderCreateDTO orderCreateDTO, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        OrderDetailVO order = orderService.createOrder(orderCreateDTO, Integer.parseInt(userId));
        return Result.success(order);
    }

    @PutMapping("/close/{orderId}")
    @ApiOperation("关闭订单 ")
    public Result closeOrder(@PathVariable("orderId") String orderId) {
        orderService.closeOrder(orderId);
        return Result.success();
    }

    @PutMapping("/paySuccess")
    public Result paySuccess(@RequestBody OrderPaySuccessDTO paySuccessDTO) {
        orderService.paySuccess(paySuccessDTO);
        return Result.success();
    }

    @PutMapping("/pay")
    @ApiOperation("支付订单")
    public Result toPayOrder(@RequestBody OrderPayDTO order) {
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        //订单支付
        model.setBody("商品描述：");
        //订单名字
        model.setSubject("杰普书城购物订单");
        //订单号
        model.setOutTradeNo(order.getOrderId());
        //过期时间
        model.setTimeoutExpress("30m");
        //订单金额
        model.setTotalAmount(order.getTotalPrice());
        //产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        //设置参数
        alipayTradePagePayRequest.setBizModel(model);
        //回调地址
        alipayTradePagePayRequest.setReturnUrl(order.getPathUrl());
        //客户端执行,拿到支付结果
        AlipayTradePagePayResponse pageExecute = null;
        try {
            pageExecute = alipayClient.pageExecute(alipayTradePagePayRequest);

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        String result = pageExecute.getBody();
        return Result.success(result);
    }

    @PutMapping("/complete/{orderId}")
    @ApiOperation("确认收货")
    public Result completeOrder(@PathVariable("orderId") String orderId) {
        orderService.completeOrder(orderId);
        return Result.success();
    }

    @GetMapping("/")
    @ApiOperation("获取用户的订单列表")
    public Result getOrders(HttpServletRequest request, OrderSearchDTO orderSearchDTO) {
        String userId = (String) request.getAttribute("userId");
        List<OrderInfoVO> vos = orderService.getOrders(Integer.parseInt(userId), orderSearchDTO);
        return Result.success(vos);
    }

    @GetMapping("/{orderId}")
    @ApiOperation("获取订单详情")
    public Result getOrderDetail(@PathVariable("orderId") String orderId) {
        OrderDetailVO vo = orderService.getOrderById(orderId);
        return Result.success(vo);
    }

}
